cmake_minimum_required(VERSION 3.15)

# Rivs toolchain (must be set before project())
set(RIVS_PATH "/opt/rivs" CACHE PATH "Path to Rivs toolkit root")
#set(CMAKE_C_COMPILER "${RIVS_PATH}/bin/rivscc")
set(CMAKE_CXX_COMPILER "${RIVS_PATH}/bin/rivscc")
project(topscv)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdinc")

# Options mirroring Makefile
set(TARGET_RIVS_ARCH "general" CACHE STRING "Target Rivs architecture: ecu300|ecu310|general")
set_property(CACHE TARGET_RIVS_ARCH PROPERTY STRINGS gcu300 gcu310 general)
option(STATIC_LINK "Link statically (use *_static libs)" OFF)

# Compute arch flag and target name
set(TARGET_NAME "unknown")
set(TARGET_RIVS_ARCH_FLAG "")
if (TARGET_RIVS_ARCH STREQUAL "ecu300")
  set(TARGET_NAME ecu300)
  set(TARGET_RIVS_ARCH_FLAG "-arch ecu300")
elseif (TARGET_RIVS_ARCH STREQUAL "ecu310")
  set(TARGET_NAME ecu310)
  set(TARGET_RIVS_ARCH_FLAG "-arch ecu310")
elseif (TARGET_RIVS_ARCH STREQUAL "general")
  set(TARGET_NAME general)
  set(TARGET_RIVS_ARCH_FLAG "-arch ecu310")
else()
  message(WARNING ">>> WARNING - unknown TARGET_RIVS_ARCH, please use ecu300 or ecu310 or general. <<<")
endif()

# Output directory similar to Makefile: ./bin/<TARGET_NAME>
set(BIN_DIR "../lib")
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BIN_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BIN_DIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BIN_DIR}")

# Include paths (avoid hardcoding user-specific FFmpeg path)
set(TOPSCV_INC_DIRS
        ${RIVS_PATH}/include
        ${RIVS_PATH}/include/ecu
        $ENV{HOME}/buildroot/include
        /opt/rivs/lib/clang/11.0.0/include
	    /opt/rivs/include/ffmpeg
        /usr/include/ecu
)
list(APPEND TOPSCV_INC_DIRS ${RIVS_PATH}/include/rivsaten)
message(STATUS "topscv_inc_dirs=${TOPSCV_INC_DIRS}")
include_directories(${TOPSCV_INC_DIRS})

# Optional extra include for FFmpeg headers if needed (e.g. avutil)
set(FFMPEG_INCLUDE_DIR "" CACHE PATH "Optional FFmpeg include directory (for avutil)")
if (FFMPEG_INCLUDE_DIR)
  include_directories("${FFMPEG_INCLUDE_DIR}")
endif()

link_directories("${RIVS_PATH}/lib")

# Global compile options
add_compile_options(-g -Wall ${TARGET_RIVS_ARCH_FLAG})

# RPATH to find Rivs shared libs at runtime when not static
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "../lib")

# Library sets
set(DYNAMIC_LIBS avfilter avcodec avformat avutil rivs pthread)
list(APPEND DYNAMIC_LIBS rivsaten)
set(STATIC_LIBS rivsrt_static efrt_static rivs_comgr_static rtcu_static efdrv_static z elf dl rt pthread)

# Find OpenCV (needed by library for debug JPEG dumps)
set(OpenCV_ROOT /root/buildroot/opencv/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)

# Build library from image_converto.cpp (no main)
add_library(topscv SHARED ../image_converto.cpp)
target_compile_options(topscv PRIVATE ${TARGET_RIVS_ARCH_FLAG})
if (STATIC_LINK)
  target_link_libraries(topscv PUBLIC ${STATIC_LIBS} ${OpenCV_LIBS})
else()
  target_link_libraries(topscv PUBLIC ${DYNAMIC_LIBS} ${OpenCV_LIBS})
  target_link_options(topscv PRIVATE -Wl,-rpath,${RIVS_PATH}/lib)
endif()
target_include_directories(topscv PUBLIC ${OpenCV_INCLUDE_DIRS})

# Build executable(s) for tests or samples
add_executable(image_converto_test ../image_converto_test.cpp)
target_link_libraries(image_converto_test PRIVATE topscv)
if (NOT STATIC_LINK)
	target_link_options(image_converto_test PRIVATE -L${RIVS_PATH}/lib -Wl,-rpath,${RIVS_PATH}/lib)
endif()

# OpenCV test
add_executable(image_converto_opencv_test ../image_converto_opencv_test.cpp)
target_link_libraries(image_converto_opencv_test PRIVATE topscv ${OpenCV_LIBS})
target_include_directories(image_converto_opencv_test PRIVATE ${OpenCV_INCLUDE_DIRS})
if (NOT STATIC_LINK)
	target_link_options(image_converto_opencv_test PRIVATE -L${RIVS_PATH}/lib -Wl,-rpath,${RIVS_PATH}/lib)
endif()

message(STATUS "RIVS_PATH=${RIVS_PATH}")
message(STATUS "TARGET_RIVS_ARCH=${TARGET_RIVS_ARCH} -> ${TARGET_RIVS_ARCH_FLAG}")
message(STATUS "Output dir: ${BIN_DIR}")
